// pages/user/empower/empowerTimes/empowerTimes.js
const common = require('../../../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        showEmpowerBuy: false,
        empowerData: null,
        buyType: 1,
        buyNum: 1
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        this.getEmpowerData()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    showEmpower: function(e) {
        this.setData({
            buyNum: 1,
            buyType: e.currentTarget.dataset.type
        })
        var animation = wx.createAnimation({
            duration: 500,
            timingFunction: "ease",
            delay: 0
        })
        this.animation = animation
        animation.translateY(1500).step()
        this.setData({
            animationData: animation.export(), // export 方法每次调用后会清掉之前的动画操作。
            showEmpowerBuy: true
        })
        setTimeout(() => {
            animation.translateY(0).step()
            this.setData({
                animationData: animation.export() // export 方法每次调用后会清掉之前的动画操作。
            })
        }, 200)
    },

    closeEmpower: function() {
        var animation = wx.createAnimation({
            duration: 500,
            timingFunction: "ease",
            delay: 0
        })
        this.animation = animation
        animation.translateY(1500).step()
        this.setData({
            animationData: animation.export(),
        })
        setTimeout(function() {
            animation.translateY(0).step()
            this.setData({
                animationData: animation.export(),
                showEmpowerBuy: false
            })
        }.bind(this), 200)
    },

    myCatchTouch: function() {
        return
    },

    getEmpowerData: function() {
        const that = this
        let data = {}
        common.post('Quota/get_quota', data).then(function(res) {
            that.setData({
                empowerData: res
            })
        }).catch(function(e) {})
    },

    selectType: function(e) {
        if (this.data.buyType == e.currentTarget.dataset.type) {
            return
        }
        this.setData({
            buyType: e.currentTarget.dataset.type
        })
    },

    add: function() {
        let num = this.data.buyNum
        num++
        this.setData({
            buyNum: num
        })
    },

    minus: function() {
        let num = this.data.buyNum
        if (num > 1) {
            num--
            this.setData({
                buyNum: num
            })
        }
    },

    goBuy: function() {
        const that = this
        wx.login({
            success(res) {
                if (res.code) {
                    //发起网络请求
                    let data = {
                        quota_goods_id: that.data.buyType,
                        num: that.data.buyNum,
                        code: res.code,
                        pay_client: 1
                    }
                    common.post('Quota/buy_quota', data).then(function(response) {
                        if (response.code == 204) {
                            wx.showToast({
                                title: '网络异常，请重试！',
                                icon: 'none',
                                duration: 1000
                            })
                            return
                        }
                        wx.requestPayment({
                            timeStamp: response.timeStamp,
                            nonceStr: response.nonceStr,
                            package: response.package,
                            signType: response.signType,
                            paySign: response.paySign,
                            success(res) {
                                that.closeEmpower()
                                that.setData({
                                    empowerData: null,
                                    buyType: 1,
                                    buyNum: 1
                                })
                                that.getEmpowerData()
                            },
                            fail(res) {}
                        })
                    }).catch(function(e) {})
                }
            }
        })
    }
})